/// 好友列表请求模型
class FriendListRequest {
  final int userId;
  final int? groupId;
  final String? status;
  final int timestamp;

  FriendListRequest({
    required this.userId,
    this.groupId,
    this.status,
    int? timestamp,
  }) : timestamp = timestamp ?? DateTime.now().millisecondsSinceEpoch ~/ 1000;

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {
      'user_id': userId,
      'timestamp': timestamp,
    };
    
    if (groupId != null) {
      data['group_id'] = groupId;
    }
    
    if (status != null && status!.isNotEmpty) {
      data['status'] = status;
    }
    
    return data;
  }
}

/// 好友信息模型
class FriendInfo {
  final int friendshipId;
  final int user1Id;
  final int user2Id;
  final int status;
  final String remark;
  final int groupId;
  final int intimacyScore;
  final String? lastInteraction;
  final String createdAt;
  final String updatedAt;
  final int friendId;
  final String username;
  final String userAvatarUrl;
  final String userSignature;
  final List<String> userTags;
  final String userPhone;

  FriendInfo({
    required this.friendshipId,
    required this.user1Id,
    required this.user2Id,
    required this.status,
    required this.remark,
    required this.groupId,
    required this.intimacyScore,
    this.lastInteraction,
    required this.createdAt,
    required this.updatedAt,
    required this.friendId,
    required this.username,
    required this.userAvatarUrl,
    required this.userSignature,
    required this.userTags,
    required this.userPhone,
  });

  factory FriendInfo.fromJson(Map<String, dynamic> json) {
    return FriendInfo(
      friendshipId: json['friendship_id'] ?? 0,
      user1Id: json['user1_id'] ?? 0,
      user2Id: json['user2_id'] ?? 0,
      status: json['status'] ?? 0,
      remark: json['remark'] ?? '',
      groupId: json['group_id'] ?? 1,
      intimacyScore: json['intimacy_score'] ?? 0,
      lastInteraction: json['last_interaction'],
      createdAt: json['created_at'] ?? '',
      updatedAt: json['updated_at'] ?? '',
      friendId: json['friend_id'] ?? 0,
      username: json['username'] ?? '',
      userAvatarUrl: json['userAvatarUrl'] ?? '',
      userSignature: json['userSignature'] ?? '',
      userTags: (json['userTags'] as List?)?.cast<String>() ?? [],
      userPhone: json['userPhone'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'friendship_id': friendshipId,
      'user1_id': user1Id,
      'user2_id': user2Id,
      'status': status,
      'remark': remark,
      'group_id': groupId,
      'intimacy_score': intimacyScore,
      'last_interaction': lastInteraction,
      'created_at': createdAt,
      'updated_at': updatedAt,
      'friend_id': friendId,
      'username': username,
      'userAvatarUrl': userAvatarUrl,
      'userSignature': userSignature,
      'userTags': userTags,
      'userPhone': userPhone,
    };
  }
}

/// 好友列表响应模型
class FriendListResponse {
  final String status;
  final int count;
  final List<FriendInfo> data;
  final String? message;

  FriendListResponse({
    required this.status,
    required this.count,
    required this.data,
    this.message,
  });

  bool get isSuccess => status == 'success';

  factory FriendListResponse.fromJson(Map<String, dynamic> json) {
    return FriendListResponse(
      status: json['status'] ?? 'error',
      count: json['count'] ?? 0,
      data: (json['data'] as List?)
          ?.map((item) => FriendInfo.fromJson(item))
          .toList() ?? [],
      message: json['message'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'count': count,
      'data': data.map((friend) => friend.toJson()).toList(),
      if (message != null) 'message': message,
    };
  }
}
